﻿using System.IO;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SharePoint.Contrib.Extensions
{
    /// <summary>
    /// Extensions for the SPFeature class
    /// </summary>
    public static class SPFeatureExtensions
    {

        /// <summary>
        /// Quick accessor to the feature´s ID as a string
        /// </summary>
        public static string Identifier(this SPFeature instance)
        {
            return instance.Definition.Id.ToString();
        }

        /// <summary>
        /// Gets the scope instance from the feature
        /// </summary>
        /// <typeparam name="T">Valid scopes are SPFarm, SPWebApplication, SPSite, SPWeb</typeparam>
        public static T Scope<T>(this SPFeature instance, SPFeatureScope scope) where T : class
        {
            T FeatureScope = default(T);

            switch (scope)
            {
                case SPFeatureScope.Farm:
                    var ws = instance.Parent as SPWebService;
                    FeatureScope = ws.Farm as T;
                    break;
                case SPFeatureScope.Web:
                    FeatureScope = instance.Parent as T;
                    if (FeatureScope == null)
                    {
                        SPSite sitecollection = instance.Parent as SPSite;
                        FeatureScope = sitecollection.RootWeb as T;
                    }
                    break;
                case SPFeatureScope.WebApplication:
                case SPFeatureScope.Site:
                    FeatureScope = instance.Parent as T;
                    break;
                case SPFeatureScope.ScopeInvalid:
                    throw "SPFeatureExtensions: Invalid feature scope"
                            .AsException();
            }

            if (FeatureScope == null)
            {
                throw "SPFeatureExtensions: FeatureScope is null. Invalid input parameters."
                        .AsException();
            }

            return FeatureScope;
        }

        /// <summary>
        /// Quick accessor to the feature directory in {SharePointRoot}/TEMPLATE/Features
        /// </summary>
        public static DirectoryInfo Directory(this SPFeature instance)
        {
            return new DirectoryInfo(instance.Definition.RootDirectory);
        }

        /// <summary>
        /// Locates a file in the feature directory.
        /// </summary>
        /// <param name="fileName">the name of the file to find (ex: myfile.xml)</param>
        public static FileInfo LocateElementFile(this SPFeature instance, string fileName)
        {
           
            var file = instance.Directory()
                        .GetFiles(fileName, SearchOption.AllDirectories)
                        .FirstOrDefault();

            if (file == null || file.Exists == false)
            {
                throw "SPFeatureExtensions: The feature '{0}' does not include a file named '{1}'"
                        .AsException(instance.Name(), fileName);
            }

            return file;
        }

        /// <summary>
        /// Quick accessor to the name of the feature (the name of the folder in {SharePointRoot}/TEMPLATE/Features)
        /// </summary>
        public static string Name(this SPFeature instance)
        {
            return instance.Definition.DisplayName;
        }
    }
}
